package com.map.controller;


import com.map.service.impl.UserServiceImpl;
import com.map.util.Result;
import com.map.util.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

/**
 * Created by Jiao on 16/4/12.
 */
@Controller
public class LoginController extends BaseController {

    private  UserServiceImpl userService;
    @Autowired
    public void setUserService(UserServiceImpl userService) {
        this.userService = userService;
    }

    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public ModelAndView getLoginView(@RequestParam("username") String username, @RequestParam("password") String password) {


        boolean flag= userService.checkUser(username,password);
        if(flag)
            return new ModelAndView("index_success");
        else
            return new ModelAndView("index");
    }
    @RequestMapping(value = "/", method = RequestMethod.GET)
    public ModelAndView index(){
        return new ModelAndView("index");
    }
    @RequestMapping(value = "/toregister",method = RequestMethod.GET)
    public ModelAndView toRegister(){
        return new ModelAndView("register");
    }
    @RequestMapping(value = "/register", method=RequestMethod.POST)
    public ModelAndView register(User user){
        userService.addUser(user);
        return new ModelAndView("index_success");
    }


    @RequestMapping(value = "/isExist",method = RequestMethod.POST)
    @ResponseBody
    public Result isExist(@RequestParam("username") String username){
        Result result = new Result();

        boolean success = userService.existUser(username);

        result.setSuccess(success);

        if(success){

            result.setMessage("<font>用户名已经存在</font>");
        }else{
            result.setMessage("<font color='green'>用户名可用</font>");
        }
        return result;
    }
}
